package com.hemaapp.yjnh.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.view.singlewheel.OnItemSelectedListener;
import com.hemaapp.yjnh.view.singlewheel.ProvinceLoopView;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 类描述：日期选择dialog
 * 创建人：GuiyouZhang
 * 创建时间：2016/6/23 13:14
 * 修改备注：
 */
public class DateSelectDialog {

    private Context context;
    private Dialog dialog;
    private Display display;
    private onSelectedItemListener listener = null;
    ArrayList<String> yearDatas = new ArrayList<>();
    ArrayList<String> monthDatas = new ArrayList<>();
    ArrayList<String> dayDatas = new ArrayList<>();

    private TextView tvCancel;
    private TextView tvOK;
    private ProvinceLoopView yearView;
    private ProvinceLoopView monthView;
    private ProvinceLoopView dayView;
    int curYear;
    int curMonth;
    int curDay;

    public DateSelectDialog(Context context) {
        this.context = context;
        Calendar calendar = Calendar.getInstance();
        curYear = calendar.get(Calendar.YEAR);
        curMonth = calendar.get(Calendar.MONTH);
        curMonth++;
        curDay = calendar.get(Calendar.DAY_OF_MONTH);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public DateSelectDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_province_city, null);
        view.setMinimumWidth(display.getWidth());
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        tvOK = (TextView) view.findViewById(R.id.tv_ok);
        yearView = (ProvinceLoopView) view.findViewById(R.id.province_loop);
        monthView = (ProvinceLoopView) view.findViewById(R.id.city_loop);
        dayView = (ProvinceLoopView) view.findViewById(R.id.county_loop);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onSelected(
                            yearDatas.get(yearView.getSelectedItem()),
                            monthDatas.get(monthView.getSelectedItem()),
                            dayDatas.get(dayView.getSelectedItem()));
                }
                dismiss();
            }
        });
        //设置滑动监听
        yearView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int year = Integer.parseInt(yearDatas.get(index));
                int month = 1;
                int startDay = 1;
                if (index == 0) {
                    month = curMonth;
                    startDay = curDay;
                }

                monthDatas.clear();
                for (int i = month; i < 13; i++) {
                    monthDatas.add(i + "");
                }
                monthView.setItems(monthDatas);
                monthView.setInitPosition(0);

                int totalDay = getDay(year, month);
                dayDatas.clear();
                for (int i=startDay;i<totalDay+1;i++) {
                    dayDatas.add(i+"");
                }
                dayView.setItems(dayDatas);
                dayView.setInitPosition(0);
            }
        });
        monthView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                dayDatas.clear();
                int year = curYear+yearView.getSelectedItem();
                int month = Integer.parseInt(monthDatas.get(index));
                int startDay = 1;
                int totalDay = getDay(year, month);
                if (year == curYear && month == curMonth)
                    startDay = curDay;
                for (int i=startDay;i<totalDay+1;i++) {
                    dayDatas.add(i+"");
                }
                dayView.setItems(dayDatas);
                dayView.setInitPosition(0);
            }
        });
        dayView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

            }
        });
        //设置不循环播放
        yearView.setNotLoop();
        monthView.setNotLoop();
        dayView.setNotLoop();

        initDate();

        int one = BaseUtil.dip2px(context, 1);
        yearView.setViewPadding(one*2, 0, one*2, 0);
        monthView.setViewPadding(one*2, 0, one*2, 0);
        dayView.setViewPadding(one*2, 0, one*2, 0);

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public void setListener(onSelectedItemListener listener) {
        this.listener = listener;
    }

    public interface onSelectedItemListener {
        public void onSelected(String year, String month, String day);
    }

    private void initDate() {
        for (int i=0;i<61;i++) {//最多60年
            yearDatas.add(curYear+i+"");
        }
        yearView.setItems(yearDatas);
        yearView.setInitPosition(0);

        for (int i=curMonth;i<13;i++) {
            monthDatas.add(i+"");
        }
        monthView.setItems(monthDatas);
        monthView.setInitPosition(0);

        int totalDay = getDay(curYear, curMonth);
        for (int i=curDay;i<=totalDay;i++) {
            dayDatas.add(i+"");
        }
        dayView.setItems(dayDatas);
        dayView.setInitPosition(0);
    }

    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }


}
